
package com.yimixian.app.address;

import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.Address;
import com.yimixian.app.util.SharedPreferencesHelper;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//dizhi
public class AddressesView extends LinearLayout {

    interface Listener {
        void onAddressClicked(Address address);

        void onAddressEditIconClicked(Address address);
    }

    private DataManager mDataManager;
    private Map<Integer, AddressView> mIdToAddressView;
    private OnClickListener mOnClickListener;
    private OnClickListener mEditIconClickListener;
    private Listener mListener;
    private boolean mDisableAddressClickListener;

    public AddressesView(Context context) {
        super(context);
        initAddressesView();
    }

    public AddressesView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAddressesView();
    }

    public AddressesView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAddressesView();
    }

    private void initAddressesView() {
        setOrientation(VERTICAL);
        mDataManager = DataManager.getInstance();
        mOnClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener == null) {
                    return;
                }

                AddressView addressView = (AddressView) v;
                addressView.getBoundAddress();
                mListener.onAddressClicked(addressView.getBoundAddress());
            }
        };
        mEditIconClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener == null) {
                    return;
                }

                Address address = mIdToAddressView.get(v.getTag()).getBoundAddress();
                mListener.onAddressEditIconClicked(address);
            }
        };
    }

    public void setListener(Listener listener) {
        mListener = listener;
    }

    public void bind(List<Address> addresses, boolean showSelectedBand) {
        removeAllViews();
        mIdToAddressView = new HashMap<>();
        if (addresses == null) {
            return;
        }
        int addressId = 0;

        Address mAddress = SharedPreferencesHelper.getAddress(SharePreferenceKeys.CURRENT_ADDRESS);
        if (mAddress != null){
            addressId = mAddress.id;
        }
        for (int i = 0; i < addresses.size(); ++i) {
            Address address = addresses.get(i);
            AddressView addressView = new AddressView(getContext());
            addressView.bind(address);
            addressView.setBottomDividerVisible(i < addresses.size() - 1);
            if (!mDisableAddressClickListener) {
                addressView.setOnClickListener(mOnClickListener);
            }
            addressView.setEditIconClickListener(mEditIconClickListener);
            if (showSelectedBand && Integer.valueOf(address.id) == addressId) {
                addressView.setSelectedBandVisible(true);
                final int postion = i;
                //滑动到当前位置
                getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                    private boolean hasScrolled = false;

                    @Override
                    public void onGlobalLayout() {
                        if (Build.VERSION.SDK_INT > 15) {
                            getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }
                        if (getParent().getParent() instanceof ScrollView && !hasScrolled) {
                            ScrollView scrollView = (ScrollView) getParent().getParent();
                            View view = getChildAt(postion);
                            int deltaY = scrollView.getHeight() - (view.getTop() + getTop() + view.getHeight());
                            scrollView.scrollTo(0, deltaY < 0 ? -deltaY : 0);
                            hasScrolled = true;
                        }
                    }
                });
            }
            mIdToAddressView.put(address.id, addressView);
            addView(addressView);
        }
    }

    public void disableAddressClickListener() {
        mDisableAddressClickListener = true;
        for (int i = 0; i < getChildCount(); ++i) {
            getChildAt(i).setClickable(false);
        }
    }
}
